﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AUV5.BLL
{
    public class AuvLevelBLL
    {
        /// <summary>
        /// 每个好评分数
        /// </summary>
        public int GoodRateScore
        {
            get
            {
                return GetGoodRateScore();
            }
        }

        /// <summary>
        /// 每个差评分数
        /// </summary>
        public int BadRateScore
        {
            get
            {
                return GetBadRateScore();
            }
        }

        /// <summary>
        /// 每个强制取消订单分数
        /// </summary>
        public int ForceScore
        {
            get
            {
                return GetForceScore();
            }
        }

        #region##读取等级配置文件
        /// <summary>
        /// 读取等级配置文件
        /// 创建人：Wilson
        /// 创建时间：2011-11-23
        /// </summary>
        /// <returns></returns>
        public System.Xml.XmlDocument ReadLevelConfig()
        {
            try
            {
                string path = System.Web.HttpContext.Current.Server.MapPath("/Config/Level.config");

                System.Xml.XmlDocument doc = System.Web.HttpContext.Current.Cache["level"] as System.Xml.XmlDocument;

                if (doc == null)
                {
                    doc = new System.Xml.XmlDocument();
                    doc.Load(path);
                    System.Web.Caching.CacheDependency cd = new System.Web.Caching.CacheDependency(path);
                    System.Web.HttpContext.Current.Cache.Insert("level", doc, cd, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromDays(1));
                }

                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region##得到等级划分
        /// <summary>
        /// 得到等级划分
        /// 创建人：Wilson
        /// 创建时间：2011-11-23
        /// </summary>
        /// <returns></returns>
        public List<AUV5.Model.AuvLevel> GetAuvLevelList()
        {

            try
            {
                List<AUV5.Model.AuvLevel> list = new List<Model.AuvLevel>();

                System.Xml.XmlDocument doc = ReadLevelConfig();

                System.Xml.XmlNodeList levelNodeList = doc.SelectNodes("auv/auvLevel/level");

                foreach (System.Xml.XmlNode node in levelNodeList)
                {
                    AUV5.Model.AuvLevel level = new Model.AuvLevel();
                    level.LevelScore = Convert.ToInt32(node.Attributes["score"].Value);
                    level.LevelUrl = node.Attributes["url"].Value;
                    list.Add(level);
                }

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region##得到好评加分规则
        /// <summary>
        /// 得到好评加分规则
        /// 创建人：Wilson
        /// 创建时间：2011-11-23
        /// </summary>
        /// <returns></returns>
        private int GetGoodRateScore()
        {
            try
            {
                System.Xml.XmlDocument doc = ReadLevelConfig();

                System.Xml.XmlNode ruleNode = doc.SelectSingleNode("auv/auvLevel");

                return Convert.ToInt32(ruleNode.Attributes["goodRate"].Value);
            }
            catch (Exception)
            {

                return 0;
            }
        }
        #endregion

        #region##得到差评加分规则
        /// <summary>
        /// 得到差评加分规则
        /// 创建人：Wilson
        /// 创建时间：2011-11-23
        /// </summary>
        /// <returns></returns>
        private int GetBadRateScore()
        {
            try
            {
                System.Xml.XmlDocument doc = ReadLevelConfig();

                System.Xml.XmlNode ruleNode = doc.SelectSingleNode("auv/auvLevel");

                return Convert.ToInt32(ruleNode.Attributes["badRate"].Value);
            }
            catch (Exception)
            {

                return 0;
            }

        }
        #endregion

        #region##得到强制取消规则
        /// <summary>
        /// 得到强制取消规则
        /// 创建人：Wilson
        /// 创建时间：2011-11-23
        /// </summary>
        /// <returns></returns>
        private int GetForceScore()
        {
            try
            {
                System.Xml.XmlDocument doc = ReadLevelConfig();

                System.Xml.XmlNode ruleNode = doc.SelectSingleNode("auv/auvLevel");

                return Convert.ToInt32(ruleNode.Attributes["force"].Value);
            }
            catch (Exception)
            {
                return 0;
            }
        }
        #endregion

        #region##通过用户ID得到用户等级对应的勋章路径
        /// <summary>
        /// 通过用户ID得到用户等级对应的勋章路径
        /// 创建人：Wilson
        /// 创建时间：2011-11-23
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public string GetUserLevelByUserId(string userId)
        {
            try
            {
                List<AUV5.Model.AuvLevel> list = GetAuvLevelList();

                if (list.Count > 0)
                {
                    int userScore = AUV5.BLL.ServiceInfoBLL.Instance.GetUserScoreByUserId(userId);

                    if (userScore < 0)
                    {
                        userScore = 0;
                    }

                    var result = list.Where(u => userScore >= u.LevelScore).OrderByDescending(u => u.LevelScore).First();

                    return result.LevelUrl;
                }
                else
                {
                    return "";
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

    }
}
